#include <algorithm>
#include <iostream>
#include <stack>
#define LL long long
using namespace std;

const int kMaxN = 1e5 + 10;

LL sum[kMaxN];
int a[kMaxN], l[kMaxN], r[kMaxN];

void GetEndPoints(int n, int ep[]) {
  stack<int> s;

  s.push(0);
  for (int i = 1; i <= n; i++) {
    for (; a[s.top()] >= a[i]; s.pop());
    ep[i] = s.top();
    s.push(i);
  }
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  GetEndPoints(n, l);
  reverse(a + 1, a + n + 1);
  GetEndPoints(n, r);
  reverse(a + 1, a + n + 1);
  reverse(r + 1, r + n + 1);
  for (int i = 1; i <= n; i++) {
    r[i] = n + 1 - r[i];
  }
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + a[i];
  }

  LL ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, (sum[r[i] - 1] - sum[l[i]]) * a[i]);
  }
  cout << ans << "\n";
  return 0;
}